package gold.digger;

import gold.utils.InputUtil;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LCJZOF005 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int maxProduct(String[] words) {
            Map<Integer, Integer> cntMap = new HashMap<>();
            for (String word : words) {
                int curBit = 0;
                for (int i = 0; i < word.length(); i++) {
                    int curCharPos = word.charAt(i) - 'a';
                    curBit |= (1 << curCharPos);
                }

                if (!cntMap.containsKey(curBit) || cntMap.get(curBit) < word.length()) {
                    cntMap.put(curBit, word.length());
                }
            }

            int ans = 0;
            List<Integer> keyList = cntMap.keySet().stream().collect(Collectors.toList());
            for (int i = 0; i < keyList.size(); i++) {
                for (int j = i + 1; j < keyList.size(); j++) {
                    if (0 == (keyList.get(i) & keyList.get(j))) {
                        ans = Math.max(ans, cntMap.get(keyList.get(i)) * cntMap.get(keyList.get(j)));
                    }
                }
            }

            return ans;
        }
    }

    public void run() {
        System.out.println(new Solution().maxProduct(new String[]{"abcw", "baz", "foo", "bar", "fxyz", "abcdef"}));
        System.out.println(new Solution().maxProduct(new String[]{"a", "ab", "abc", "d", "cd", "bcd", "abcd"}));
        System.out.println(new Solution().maxProduct(new String[]{"a", "aa", "aaa"}));
    }

    public static void main(String[] args) throws Exception {
        LCJZOF005 an = new LCJZOF005();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
